package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工相关接口")//对类的描述
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "员工登录")//对方法的描述
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("退出")
    public Result<String> logout() {
        return Result.success();
    }


    /**
     * 添加员工
     * @param employeeDTO
     * */
    @PostMapping
    @ApiOperation("添加员工")
    public Result<String> employee(@RequestBody EmployeeDTO employeeDTO){
//        String token = request.getHeader(jwtProperties.getAdminTokenName());
//        String adminSecretKey = jwtProperties.getAdminSecretKey();
        employeeService.addEmployee(employeeDTO);
        return Result.success("添加成功");
    }
    @GetMapping("/{id}")
    @ApiOperation("根据id查询员工")
    public Result<Employee> selectEmployeeById(@PathVariable @ApiParam("员工id") Long id){
        Employee employee = employeeService.selectEmployeeById(id);
        employee.setPassword("******");
        if (employee==null){
            return Result.error("id为"+id+"的用户不存在");
        }
        return Result.success(employee);
    }

    /**
     * 员工分页查询
     * @param employeePageQueryDTO
     * @return
     * */
    @GetMapping("/page")
    @ApiOperation("员工分页查询")
    public Result<PageResult> employeePageQuery(EmployeePageQueryDTO employeePageQueryDTO){
        PageResult pageResult = employeeService.employeePageQuery(employeePageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 修改密码
     * @param passwordEditDTO
     * */
    @PutMapping("/editPassword")
    @ApiOperation("修改密码")
    public Result editPassword(@RequestBody PasswordEditDTO passwordEditDTO){
        employeeService.editPassword(passwordEditDTO);
        return Result.success();
    }

    /**
     * 启用、禁用员工账号
     * @param status,id
     *
     * */
    @PostMapping("/status/{status}")
    @ApiOperation("启用、禁用员工账号")
    public Result changeStatusByID(@PathVariable("status") Integer status,Long id){
        employeeService.updateStatus(status,id);
        return Result.success();
    }
    /**
     * 编辑员工信息
     * @param
     * */
    @PutMapping()
    @ApiOperation("编辑员工信息")
    public Result editEmployee(@RequestBody EmployeeDTO employeeDTO){

        Result result = employeeService.editEmployee(employeeDTO);
        return result;
    }


}






































